package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.AdminMapper;
import edu.gxnu.cetcdemo.pojo.Admin;
import edu.gxnu.cetcdemo.service.AdminService;
import edu.gxnu.cetcdemo.utils.JwtHelper;
import edu.gxnu.cetcdemo.utils.MD5Util;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2024-04-17 12:24:27
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(Admin admin) {
        // 查询数据库
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Admin loginAdmin = adminMapper.selectOne(lambdaQueryWrapper.eq(Admin::getAdminAccount, admin.getAdminAccount()));

        // 用户名不存在
        if (loginAdmin == null) {
            return Result.build(501, "管理员账户不存在");
        }else{
            // 4.17 13:24 修改：管理员登录不加密
            if (admin.getAdminPassword().equals(loginAdmin.getAdminPassword())) {
                // 登录成功
                // 生成token
                String token = JwtHelper.getToken((loginAdmin.getAdminId()).toString());
                // 返回管理员数据和token
                Map data = new HashMap();
                data.put("token", token);
                data.put("admin", loginAdmin);
                return Result.build(200, "登录成功", data);
            }else{
                // 登录失败
                return Result.build(502, "密码错误");
            }

        }
    }

    @Override
    public Result addAdmin(Admin admin) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Admin tadmin = adminMapper.selectOne(lambdaQueryWrapper.eq(Admin::getAdminName, admin.getAdminName()));

        if(tadmin != null){
            return Result.build(501, "管理员已存在");
        }

        adminMapper.insert(admin);
        return Result.build(200, "添加管理员成功");
    }

    @Override
    public Result queryAdminList() {
        Map data = new HashMap();
        List<Admin> list = adminMapper.selectList(null);
        data.put("adminList", list);
        return Result.build(200, "成功查询所有管理员信息", data);
    }

    @Override
    public Result delAdmin(long adminId) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        adminMapper.delete(lambdaQueryWrapper.eq(Admin::getAdminId, adminId));
        return Result.build(200, "删除管理员成功");
    }

    @Override
    public Result updateAdmin(Admin admin) {
        adminMapper.update(admin, new LambdaQueryWrapper<Admin>().eq(Admin::getAdminId, admin.getAdminId()));
        return Result.build(200, "管理员信息修改成功");
    }
}




